﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WordRacer.Domain.Model.Dictionaries;
using System.IO;

namespace WordRacer.Domain.Tests.Integration.Fixtures
{
  public static class DictionaryFixture
  {

    public static IGameDictionary CreateDictionaryAndAddWordsFromFile(FileInfo fileInfo)
    {
      //create the dictionary
      var dictionaryFactory = new DictionaryFactory();
      var newGameDictionary = dictionaryFactory.CreateGameDictionary();
      var returnGameDictionary = AddWordsToDictionaryFromFile(newGameDictionary, fileInfo);
      return returnGameDictionary;
    }

    public static IGameDictionary UseProvidedDictionaryAndAddWordsFromFile(IGameDictionary gameDictionary, FileInfo fileInfo)
    {
      //assign the dictionary
      var incomingGameDictionary = gameDictionary;
      var returnGameDictionary = AddWordsToDictionaryFromFile(incomingGameDictionary, fileInfo);
      return returnGameDictionary;
    }

    private static IGameDictionary AddWordsToDictionaryFromFile(IGameDictionary gameDictionary, FileInfo fileInfo)
    {
      //assign the dictionary
      var returnGameDictionary = gameDictionary;
      //open the file for reading
      var fileStream = fileInfo.OpenRead();
      var streamReader = new System.IO.StreamReader(fileStream);
      string line;

      //load dictionary from filestreamreader
      while ((line = streamReader.ReadLine()) != null)
      {
        line = line.Trim();
        returnGameDictionary.AddWord(line);
      }

      //close the stream and the file
      streamReader.Close();
      fileStream.Close();

      return returnGameDictionary;
    }

  }
}
